package org.practice;

import java.util.Scanner;

/**
 * @author zch
 */
public class Count {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String msg = sc.nextLine();
        System.out.println("利用正则表达式解答：");
        countWithRegular(msg);
        System.out.println("遍历计数：");
        count(msg);
    }

    public static void countWithRegular(String msg) {
        int len = msg.length();
        msg = msg.replaceAll("\\d", "");
        System.out.println("数字: " + (len - msg.length()) +"个" );
        len = msg.length();
        msg = msg.replaceAll("[a-z]|[A-Z]", "");
        System.out.println("字母: " + (len - msg.length()) +"个" );
        len = msg.length();
        msg = msg.replaceAll("\\s", "");
        System.out.println("空格: " + (len - msg.length()) +"个" );
        len = msg.length();
        System.out.println("其它: " + len  +"个");
    }

    public static void count(String msg) {
        int num = 0, letter=0, space = 0, other = 0;
        char[] chars = msg.toCharArray();
        for (char c : chars) {
            if (Character.isWhitespace(c)) {
                space++;
            } else if (Character.isLetter(c)) {
                letter++;
            } else if (Character.isDigit(c)) {
                num++;
            } else {
                other++;
            }
        }
        System.out.println("数字: " + num +"个" );
        System.out.println("字母: " + letter +"个" );
        System.out.println("空格: " + space +"个" );
        System.out.println("其他: " + other +"个" );
    }

}
